//数据类型-字段
const data_type_strs = "Boolean Number String Function Array Date RegExp Object Error Symbol";
const class2type = {};
const toString = class2type.toString;
data_type_strs.split(' ').forEach((name,i)=>{
  class2type[`[object ${name}]`] = name.toLowerCase();
});
//extend
let extend = function () {
  var options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},
    i = 1,
    length = arguments.length,
    deep = false;

  //--- extend(true||false,{},{}...)
  if (typeof target === "boolean") {
    deep = target;

    target = arguments[i] || {};

    i++;
  }

  //--- 第1个参数是bool值，target不是{}时
  if (typeof target !== "object" && target !== "function") {
    target = {};
  }

  //---
  for (; i < length; i++) {

    if ((options = arguments[i]) != null) {

      for (name in options) {
        src = target[name],
          copy = options[name];

        if (target === copy) {
          continue;
        }

        //--- 深复制
        if (deep && copy && (copy.constructor === Object || (copyIsArray = Array.isArray(copy)))) {
          if (copyIsArray) {
            copyIsArray = false;
            clone = src && Array.isArray(src) ? src : [];
          } else {
            clone = src && src.constructor === Object ? src : {};
          }

          target[name] = extend(deep, clone, copy);
        } else if (copy !== undefined) {
          target[name] = copy;
        }
      }
    }
  }

  return target;
};
//addZero
let addZero = function (num) {
  return num < 10 ? '0' + num : '' + num;
};
//是否为空对象
let isEmptyObj = function(obj){
  if(!obj||typeof obj==='undefined')return true;
  let result = true;
  for (const key in obj) {
    if(obj.hasOwnProperty(key)){
      result = false;
      break;
    }
  }
  return result;
};
//节流
let debounce = function(fn,time){ //节流
  var __self = fn,
      timer,
      firstTime = true;
  return function(){
    var args = arguments,
        __me = this;
    if(firstTime){
      __self.apply(__me,args);
      //return firstTime = false;
      firstTime = false
      return
    }
    if(timer){
      //return false;
      return
    }
    timer = setTimeout(function(){
      clearTimeout(timer);
      timer = null;
      __self.apply(__me,args);
    },time||200);
  };
}

module.exports = {
  toString,
  data_type_strs,
  extend,
  addZero,
  isEmptyObj,
  debounce
};